﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using System.Globalization;
using System.Threading;
using Microsoft.ApplicationBlocks.Data;


namespace AnyOffice.general.calendar.manage
{
    public partial class arrange : ModulePage
    {
        private Thread thread;
        private CultureInfo cultureInfo;

        protected void DataCalendar1_SelectionChanged(object sender, EventArgs e)
        {
            DateTime selectedDate = this.DataCalendar1.SelectedDate;
            Response.Redirect("manage.aspx?date=" + selectedDate.Year.ToString() + "-" + selectedDate.Month.ToString() + "-" + selectedDate.Day.ToString());
        }
   
        private DataTable GetData(DateTime startDate, DateTime endDate)
        {
            DataSet dataSet = new DataSet();
            SqlHelper.FillDataset(base.Conn, CommandType.Text, "Select *,dbo.GetUserName(ManagerId) AS ManagerUserName From Calendar Where StartTime >= '" + startDate.ToString() + "' And StartTime <= '" + endDate.ToString() + "' and UserId='" + base.User.Identity.Name + "' Order by StartTime ASC", dataSet, new string[] { "Calendar" });
            return dataSet.Tables[0];
        }
        private void DataCalendar1_PreRender(object sender, EventArgs e)
        {
            this.thread = Thread.CurrentThread;
            this.cultureInfo = this.thread.CurrentCulture;
            CultureInfo info = (CultureInfo)this.cultureInfo.Clone();
            info.DateTimeFormat.DayNames = new string[] { "日", "一", "二", "三", "四", "五", "六" };
            info.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
            this.thread.CurrentCulture = info;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.selectYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
                this.selectMonth.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
                this.DataCalendar1.VisibleDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                DateTime startDate = new DateTime(this.DataCalendar1.VisibleDate.Year, this.DataCalendar1.VisibleDate.Month, 1).AddDays(-7.0);
                DateTime endDate = new DateTime(this.DataCalendar1.VisibleDate.Date.AddMonths(1).Year, this.DataCalendar1.VisibleDate.Date.AddMonths(1).Month, 1).AddDays(7.0);
                this.DataCalendar1.DataSource = this.GetData(startDate, endDate);
                this.DataCalendar1.DataBind();
            }
            if (this.Page.IsPostBack)
            {
                this.DataCalendar1.VisibleDate = new DateTime(Convert.ToInt32(this.selectYear.Value), Convert.ToInt32(this.selectMonth.Value), 1);
                DateTime time20 = new DateTime(this.DataCalendar1.VisibleDate.Year, this.DataCalendar1.VisibleDate.Month, 1).AddDays(-7.0);
                DateTime time28 = new DateTime(this.DataCalendar1.VisibleDate.Date.AddMonths(1).Year, this.DataCalendar1.VisibleDate.Date.AddMonths(1).Month, 1).AddDays(7.0);
                this.DataCalendar1.DataSource = this.GetData(time20, time28);
                this.DataCalendar1.DataBind();
            }
        }
        protected override void OnInit(EventArgs e)
        {
            this.DataCalendar1.PreRender += new EventHandler(this.DataCalendar1_PreRender);
            base.OnInit(e);
        }


 
    }
}
